/*
 * Copyright (c) 2009-2011, Petr Panteleyev <petr@panteleyev.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient.profwizard;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.panteleyev.blogclient.MainClass;
import org.panteleyev.flickrapi.AuthResult;
import org.panteleyev.flickrapi.FlickrAccount;
import org.panteleyev.flickrapi.FlickrException;
import org.panteleyev.flickrapi.FlickrPermission;
import org.panteleyev.swing.OSUtil;

/**
 *
 * @author Petr Panteleyev <petr@panteleyev.org>
 */
public class FlickrAuthPanel extends JPanel implements IWizardPage {
    private javax.swing.JButton authButton;

    private boolean stepTwo = false;
    private FlickrAccount acc;
    private String frob;
    private AuthResult authResult;

    public FlickrAuthPanel() {
        initComponents();
    }

    public AuthResult getAuthResult() {
        return authResult;
    }

    private void onAuthorize() {
        if (stepTwo) {
            try {
                authResult = acc.requestToken(frob);
                authButton.setText("Done");
                authButton.setEnabled(false);
            } catch (IOException | FlickrException ex) {
                MainClass.handleException(null, ex);
            }
        } else {
            acc = new FlickrAccount();

            try {
                frob = acc.requestFrob();
                String url = acc.getAuthUrl(frob, FlickrPermission.WRITE);
                OSUtil.runBrowser(url);
                authButton.setText("Complete Authorization");
                stepTwo = true;
            } catch (FlickrException | IOException ex) {
                MainClass.handleException(null, ex);
            }
        }
    }

    private void initComponents() {
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));

        add(new JLabel("<html><h2><b>This program requires your authorization<br>before it can read or modify your photos<br>" +
                "and data on Flickr.</b></h2>Authorizing is a simple process which takes place in your<br>" +
                "web browser. When you are finished, return to this window<br>to complete authorization.</html>"));

        add(authButton = new JButton(new AbstractAction("Authorize...") {
            @Override
            public void actionPerformed(ActionEvent e) {
                onAuthorize();
            }
        }));
    }

    @Override
    public boolean isNextAllowed() {
        return authResult != null;
    }

    @Override
    public String getPrevPage() {
        return NewProfileWizard.ACCOUNT_TYPE_PAGE;
    }

    @Override
    public String getNextPage() {
        return NewProfileWizard.PROFILE_OPTIONS_PAGE;
    }

}
